<script>
	import QQMapWx from '@/common/qqmap-wx-jssdk.min.js'
	const qqmapSdk = new QQMapWx({
		key: 'FWEBZ-WHSHV-IRFPO-UNMRL-5EUWV-BFBFW' // 在腾讯地图上申请的key
	})
	export default {
		onLaunch: function() {
			let vm = this;
			uni.addInterceptor('navigateTo', { //监听跳转
				success(e) {
					console.log('navigateTo');
					vm.$store.dispatch('getMessageCount')
				}
			})
			uni.addInterceptor('redirectTo', { //监听跳转
				success(e) {
					console.log('redirectTo');
					vm.$store.dispatch('getMessageCount')
				}
			})
			this.init();
		},
		onShow: function() {
			console.log('onShowonShow');
		},
		onHide: function() {

		},
		methods: {
			async init() {
				await this.getSystemInfo();
				 /*#ifdef MP-WEIXIN*/
				await this.getAuthorize();
				await this.getLocation();
				/*#endif*/
				await this.$store.dispatch('getConfigInfo')
				await this.$store.dispatch('getTagInfo')
				await this.$store.dispatch('getServeData')
				await this.$store.dispatch('getGiftData')
				await this.$store.dispatch('getAreaData')	
			},
			getSystemInfo() {
				return new Promise((resolve, reject) => {
					var _this = this;
					uni.getSystemInfo({
						success: (systemInfo) => {
							this.$store.commit('setSystemInfo', systemInfo)
							resolve()
						}
					})
				})
			},
			getAuthorize() {
				return new Promise((resolve, reject) => {
					let vm = this;
					uni.authorize({
						scope: 'scope.userLocation',
						success: (res) => {
							// this.getLocation()
							resolve()
						},
						fail: (err) => {
							uni.showModal({
								content: '需要授权位置信息',
								confirmText: '确认授权',
								success(res) {
									console.log('cancelcancelcancelcancel', res);
									if (res.confirm) {
										uni.openSetting({
											success: res => {
												if (res.authSetting[
														'scope.userLocation']) {
													uni.showToast({
														title: '授权成功',
														icon: 'none'
													})
												} else {
													uni.showToast({
														title: '授权失败',
														icon: 'none'
													})
												}
												resolve()
											}
										})
									}else {
										resolve()
									}
								}
							})
						}
					})
				})
			},
			// 4、开始获取定位方法
			getLocation() {
				console.log('resolveresolveresolveresolveresolveresolveresolveresolve');
				return new Promise((resolve, reject) => {
					let vm = this;
					uni.getLocation({
						type: 'gcj02',
						success: (res) => {
							const {
								latitude,
								longitude
							} = res
							qqmapSdk.reverseGeocoder({
								location: latitude ? `${latitude},${longitude }` : '',
								sig: 'FWEBZ-WHSHV-IRFPO-UNMRL-5EUWV-BFBFW', // 这个sig就是上面要准备的第二项SK
								success: (val) => {
									console.log('这就是要获取的当前所在城市的相关信息', val)
									vm.$store.commit('setCityInfo', val.result.ad_info);
									vm.$store.dispatch('getAreaInfo')
									resolve()
								},
								fail: (err) => {
									console.log('获取城市失败')
									resolve('获取城市失败')
								}
							})
						},
						fail: (err) => {
							console.log('err',err);
							if (err.errMsg ===
								'getLocation:fail:ERROR_NOCELL&WIFI_LOCATIONSWITCHOFF' || err
								.errMsg === 'getLocation:fail system permission denied') {
								uni.showModal({
									content: '请开启手机定位服务',
									showCancel: false
								})
								resolve('请开启手机定位服务')
							} else if (err.errMsg === 'getLocation:fail:system permission denied') {
								uni.showModal({
									content: '请给微信开启定位权限',
									showCancel: false
								})
								resolve('请给微信开启定位权限')
							}
							
							resolve()
						}
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uni_modules/uview-ui/index.scss";
	@import "common/demo.scss";
	
	@import "static/css/common.scss";
	@import "static/css/flex.scss";
	@import 'static/css/iconfont.css';
	button {
		padding: 0;
		margin: 0;
	}
	button::after {
		border: 0;
		border-radius: 0;
	}
</style>
